<template>
  <basic-dialog
    :visible="dialogVisible"
    :title="'价格管理周期-处理意见填写'"
    :width="'50%'"
    :handleClose="handleCloseDialog"
    :handleOpen="handleOpenDialog"
    :handleOk="handleSubmit"
    :loadingBtn="submitBtnLoading"
    :okText="'提交'"
  >
    <el-form
      ref="auditForm"
      :model="auditForm"
      :rules="formValidate"
      label-width="120px"
      v-loading="formLoading"
    >
      <el-form-item label="供应商编码:">
        <span>{{auditForm.supplyUuid}}</span>
      </el-form-item>
      <el-form-item label="供应商名称:">
        <span>{{auditForm.supplyName}}</span>
      </el-form-item>
      <el-form-item label="价格管理周期:">
        <span>{{auditForm.priceManagementCycle}}</span>
      </el-form-item>
      <el-form-item label="是否需调整进价" prop="adjustPriceState">
        <el-select v-model="auditForm.adjustPriceState">
          <el-option
            v-for="item in PRICE_STATE_OPTIONS"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
        <el-button
          style="margin-left: 5px"
          v-if="auditForm.adjustPriceState == 2"
          type="primary"
          @click="handleViewDetail"
        >查看明细</el-button>
      </el-form-item>
      <el-form-item label="处理备注说明:" prop="processRemark">
        <el-input v-model.trim="auditForm.processRemark" maxlength="200" type="textarea" :rows="6" />
      </el-form-item>
    </el-form>
  </basic-dialog>
</template>
<script>

import BasicDialog from '@/components/BasicDialog';
import { PRICE_STATE_OPTIONS } from '../constants'
import { handleProcessPriceCycleService } from '@/api/finance/priceCycleManage/toDoList';
import { handleOpenNewPage } from '@/utils/bizUtils';

export default {
  name: 'DisposeDialog', // 处理待办
  components: {
    BasicDialog
  },
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    rowData: {
      type: Object,
      default: () => {
        return {}
      }
    }
  },
  data () {
    return {
      formLoading: false,
      submitBtnLoading: false,
      PRICE_STATE_OPTIONS,
      auditForm: {
        recordNo: '',
        adjustPriceState: '', // 1 是 2 否
        processRemark: '',
        priceManagementCycle: '',
      },
      formValidate: {
        adjustPriceState: [
          { required: true, message: '请选择是否需调整进价', trigger: 'change' }
        ],
        processRemark: [
          { required: true, message: '请输入处理备注说明', trigger: 'blur' },
          { type: 'string', min: 1, max: 200, message: '最多输入200字', trigger: 'blur' }
        ],
      },
    };
  },
  computed: {
    dialogVisible () {
      return this.visible;
    },
  },
  methods: {
    handleSubmit () {
      this.$refs.auditForm.validate((valid) => {
        if (valid) {
          if (this.auditForm.adjustPriceState == 1) {
            this.handleSubmitAction()
          } else {
            this.$alert('请确认该供应商需调价商品已经调价完毕', '提示', {
              confirmButtonText: '确定',
              callback: () => {
                this.handleSubmitAction()
              }
            });
          }
        }
      });
    },
    async handleSubmitAction () {
      const { adjustPriceState, processRemark, recordNo } = this.auditForm
      const data = {
        recordNo,
        adjustPriceState,
        processRemark
      }
      const res = await handleProcessPriceCycleService(
        data,
        state => { this.submitBtnLoading = state }
      );
      if (res) {
        this.$message.success('提交成功')
        // 是调整进价
        this.$emit('disposeCallback', { adjustPriceState });
        this.handleCloseDialog()
      }
    },
    // 获取详情数据
    getDetailData () {
      console.log(this.rowData)
      const {
        recordNo,
        supplyName,
        supplyUuid,
        adjustPriceState,
        priceManagementCycle,
        processRemark,
      } = this.rowData;
      this.auditForm = {
        recordNo,
        supplyName,
        supplyUuid,
        adjustPriceState,
        processRemark,
        priceManagementCycle,
      };
    },
    // 打开弹窗
    handleOpenDialog () {
      this.getDetailData()
    },
    // 关闭弹框
    handleCloseDialog () {
      this.$emit('update:visible', false);
    },
    // 查看明细
    handleViewDetail () {
      handleOpenNewPage(this, { name: 'todoSupplyDetail', query: { recordNo: this.auditForm.recordNo, state: '1' } })
    },
  }
};
</script>
